package com.yami.shop.service.impl;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.SysAccessKey;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.dao.SysAccessKeyMapper;
import com.yami.shop.service.SysAccessKeyService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * 系统访问密钥
 *
 * @author YXF
 * @date 2021-10-19 14:41:38
 */
@Service
@AllArgsConstructor
public class SysAccessKeyServiceImpl extends ServiceImpl<SysAccessKeyMapper, SysAccessKey> implements SysAccessKeyService {

    private final SysAccessKeyMapper sysAccessKeyMapper;

    private final Snowflake snowflake;

    @Override
    public SysAccessKey getBySysAccessKeyId(Long sysAccessKeyId) {
        return sysAccessKeyMapper.selectById(sysAccessKeyId);
    }

    @Override
    @CacheEvict(cacheNames = "sysAccessKey", key = "#sysAccessKey.accessId")
    public void saveSysAccessKey(SysAccessKey sysAccessKey) {
        sysAccessKey.setAccessId(snowflake.nextIdStr());
        Digester sha256 = new Digester(DigestAlgorithm.SHA256);
        sysAccessKey.setAccessKey(sha256.digestHex(sysAccessKey.getAccessId()));
        Date date = new Date();
        sysAccessKey.setCreateTime(date);
        sysAccessKey.setUpdateTime(date);
        save(sysAccessKey);
    }

    @Override
    public void resetAccessKey(Long sysAccessKeyId) {
        SysAccessKey sysAccessKey = sysAccessKeyMapper.selectById(sysAccessKeyId);
        if (Objects.isNull(sysAccessKey)) {
            // 此数据已被删除
            throw new YamiShopBindException("yami.shop.data.is.removed");
        }
        Digester sha256 = new Digester(DigestAlgorithm.SHA256);
        sysAccessKey.setAccessKey(sha256.digestHex(sysAccessKey.getAccessId()));
        Date date = new Date();
        sysAccessKey.setUpdateTime(date);
        updateById(sysAccessKey);
    }

    @Override
    @CacheEvict(cacheNames = "sysAccessKey", key = "#sysAccessKey.accessId")
    public void updateSysAccessKey(SysAccessKey sysAccessKey) {
        SysAccessKey accessKey = new SysAccessKey();
        accessKey.setSysAccessKeyId(sysAccessKey.getSysAccessKeyId());
        accessKey.setName(sysAccessKey.getName());
        accessKey.setRemark(sysAccessKey.getRemark());
        accessKey.setUpdateTime(new Date());
        updateById(accessKey);
    }

    @Override
    @CacheEvict(cacheNames = "sysAccessKey", key = "#accessId")
    public void removeCacheById(String accessId) {

    }

    @Override
    @Cacheable(cacheNames = "sysAccessKey", key = "#accessId")
    public SysAccessKey getByAccessId(String accessId) {
        SysAccessKey sysAccessKey = sysAccessKeyMapper.selectOne(new LambdaQueryWrapper<SysAccessKey>().eq(SysAccessKey::getAccessId, accessId));
        if (Objects.isNull(sysAccessKey)) {
            sysAccessKey = new SysAccessKey();
        }
        return sysAccessKey;
    }
}
